<?php
/* ******************************
	Incoming Tasks aJax actions
   ******************************
*/
include('../../includes/classes/core.php');
$action 	= $_REQUEST['act'];
$data 		= '';
$error		= '';
$start		= $_REQUEST['start'];
$end		= $_REQUEST['end'];
$client_id	= $_REQUEST['id'];
switch ($action) {
	case 'get_list':
	  $data=get_list();
	 break;
	case 'dialog':
	     $data[page]=getpage();
	break;
    default:
	       $error = "action null";
	}

$data['error'] = $error;
echo json_encode($data);

function get_list() {

    $columns    = $_REQUEST[columns];
    $rorder     = $_REQUEST[order];
    $search     = $_REQUEST[search];
    if ($_REQUEST[length]>1) {
        $limit="\n LIMIT $_REQUEST[start],$_REQUEST[length] \n";
    }

    $query=("SELECT	 e_letter_details.warehouse_detail_id,
            				 IF(SUBSTR(barcode, 1, 1)=1, 'რეზერვი', 'კლიენტი') AS `type`,
            				 e_letter_details.barcode,
            				 production.`name`,
                             CONCAT('ე - ' , d_letter_id),
            				 d_letter.fact_end_date,
            				 client.`name`,
            				 printer_parts_type.`name` AS `service`,
            				 price,
            				 self_cost AS self
            FROM `e_letter_details`
            JOIN  production ON SUBSTR(barcode, 6, 4) = production.id
            JOIN  d_letter   ON e_letter_details.d_letter_id=d_letter.id
            JOIN  client ON SUBSTR(barcode, 2, 4) = client.id
            JOIN	printer_parts_type ON printer_parts_type.id = part_type_id
	        where true    ");
    $data[recordsFiltered] = mysql_num_rows(mysql_query($query));

    $arr=explode("\n", $query);

    foreach ($columns as &$value) {

        $val = $value[search][value];
        $key= $value[data];
        $g_v=$search[value];
        $filt=substr_replace($arr[$key], '', -2);
        $filt=explode('AS', $filt );

        if ($key>0){ //* ძებნა გლობალური
            $g_searc.="$or CONVERT($filt[0],char) like '%$g_v%' \n";
            $or='OR';
        }

        if($val!=''){  //* ძებნა ლოკალური
            $l_searc.=" AND CONVERT($filt[0],char) like '%$val%' \n";
        }    }
        $key = $rorder[0][column];
        $met = $rorder[0][dir];

        if ($key>0){ //* სორტირება
            $filt=substr_replace($arr[$key], '', -2);
            $filt=explode('AS', $filt );
            $order="ORDER BY  $filt[0] $met";}

            $query.="$l_searc";
            if ($g_searc!='') {
                $query.="and ($g_searc)";
            }

            $query.=$order;
            $query.=$limit;
            $rResult=mysql_query($query);
        //    echo $query;
            $data[recordsTotal] = mysql_num_rows($rResult);


            if(mysql_num_rows($rResult)>0){

                while ( $aRow = mysql_fetch_array( $rResult ) )
                {

                    $data['data'][] = $aRow;
                }
            }else{
                $data['data'] = '';
            };
            return $data;

}
function GetPage($rec_id){
    $data = '
	  <div id="dialog-form">
	    <fieldset>
	    	<legend>ძირითადი ინფორმაცია</legend>
			<table border="0" width="400px">
				<tr align="center">
					<th align="center" width=""><label for="">კლიენტი</label></th>
					<th align="center"><label for="object">შედგენის თარიღი</label></th>
				</tr>
				<tr align="center">
					<th align="center"> <input type="text" class="idls" id="OBJ" value="' . $aRow[OBJ] . '" style="width: 350px;"  /></th>
					<th align="center">	<input type="text" class="idls" id="create_date" value="' . $aRow[create_date] . '"  /></th>
				</tr>
			</table>
			<table border="0" width="400px">
			    <tr align="center">
					<th align="center" width=""><label for="">შტრიხკოდი</label></th>
					<th align="center"><label for="object">მოდელი</label></th>
					<th align="center"><label for="object">ბარათის ნომერი</label></th>
				</tr>
				<tr align="center">
					<th align="center"> <input type="text" id="barcode" class="idls" value="' . $aRow[barcode] . '" /></th>
					<th align="center">	<input type="text" id="prod_name" class="idls" value="' . $aRow[prod_name] . '" /></th>
					<th align="center">	<input type="text" id="latter" class="idls" value="' . $aRow[latter] . '" /></th>
				</tr>
				<tr align="center">
					<th align="center" width="300px"><label for="">მიტანის თარიღი</label></th>
					<th align="center" width=""><label for="">უმცროსი ტექნიკოსი</label></th>
					<th  align="center"><label for="object">უფროსი ტექნიკოსი</label></th>
				</tr>
				<tr align="center">
					<th align="center"> <input type="text" id="end_date" class="idls" value="' . $aRow[end_date] . '" /></th>
					<th align="center">	<input type="text" id="junior_technician_id" class="idls" value="' . $aRow[junior_technician_id] . '" /></th>
					<th align="center">	<input type="text" id="senior_technician_id" class="idls" value="' . $aRow[tester_id] . '" disabled/></th>
				</tr>
				<tr align="center">
					<th align="center" width="300px"><label for="">შეავსო</label></th>
					<th align="center" width=""><label for="">საწყისი წონა</label></th>
					<th  align="center"><label for="object">არსებული წონა</label></th>
				</tr>
				<tr align="center">
					<th align="center"><input id="filler_id" class="idls " value="'. $aRow[filler_id] .'" disabled/></th>
					<th align="center"><input type="number" id="first_weight"   min="0" max="1" step="0.001" class="idls" value="'. $aRow[quantity] .'"></th>
					<th align="center"><input type="number" id="current_weight" min="0" max="1" step="0.001" class="idls" value="'. $aRow[current_weight] .'"></th>
				</tr>
			</table>
        </fieldset>

		<fieldset>
			<table border="0" width="400px">
				<tr align="center">
					<th align="center" width="442px; "><label for="">ხარვეზის აღწერილობა</label></th>
					<th align="center"><label for="object">შეკეთების აღწერა</label></th>
				</tr>
				<tr align="center">
					<th align="center"><textarea style="width: 342px; height: 50px;" class="idls" id="comment" >' . $aRow[comment] . '</textarea></th>
					<th align="center"><textarea style="height: 50px;"  class="idls"  disabled>' . $aRow2 . '</textarea></th>
				</tr>
			    <tr align="center">
					<th align="center"><input type="button" id="disable" style="display: inline-block;" class="idls" value="საგარანტიოს გაუქმება" /><input type="button" style="display: inline-block;"  id="print" class="idls" value="ბეჭდვა" /></th>
					<th align="center"></th>
				</tr>
			</table>

		</fieldset>
		<input type="hidden" id="this_id" value="'.$_REQUEST[id].'" />
		<input type="hidden" id="act" value="save-dialog" />
    </div>
    ';

    return $data;
}

?>